package com.lirh.mysql.controller;

import com.lirh.mysql.dao.User1Dao;
import com.lirh.mysql.entity.User1Entity;
import com.lirh.mysql.entity.User2Entity;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user1")
public class User1Controller {
    @Autowired
    private User1Dao user1Dao;
    @GetMapping("/test")
    public String test(){
        return "hello lirh!";
    }

    @GetMapping("/{id}")
    @Cacheable(cacheNames = "user1",key = "#id",unless = "#result==null")
    public User1Entity get(@PathVariable("id")Integer id){
        System.out.println("toget");
       return user1Dao.toGet(id);
    }

    @PostMapping("/add")
    @CachePut(cacheNames = "user1",key = "#entity.id")
    public String post(@RequestBody User1Entity entity){
        System.out.println("to post");
        user1Dao.toinsert(entity);
        return " insert ok";
    }
    @PostMapping("/update")
    @CachePut(cacheNames = "user1",key = "#entity.id")
    public String update(@RequestBody User1Entity entity){
        System.out.println("to update");
        user1Dao.toupdate2(entity);
        return " update ok";
    }
    @GetMapping("/del/{id}")
    @CacheEvict(cacheNames = "user1",key = "#id")
    public String del(@PathVariable("id") Integer id){
        System.out.println("to Del");
        user1Dao.todele(id);
        return "del ok";
    }
}
